package com.mengtian_tech.tju.dataquery.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mengtian_tech.tju.dataquery.api.model.tjuOracle.Employee;
import com.mengtian_tech.tju.dataquery.api.service.EmployeeService;
import com.mengtian_tech.tju.dataquery.api.service.PermissionService;
import com.mengtian_tech.tju.dataquery.api.util.ResponseUtil;
import com.mengtian_tech.tju.dataquery.api.util.SessionUtil;
import com.mengtian_tech.tju.dataquery.api.vo.User;


@RestController
@RequestMapping("/api/sys")
public class SysController {
	
	private Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	EmployeeService employeeService;
	
	@Autowired
	PermissionService permissionService;
	

	 
	 
	
	 
	/**
	 * 当前用户或者为空
	 */
	@GetMapping("/current_user")
	public Object currentUser(HttpServletRequest req) {
		String staff = req.getRemoteUser();
		Employee employee = employeeService.findByStaffId(staff);
		
		User user = new User();
		List<String> list = permissionService.getUserPerms(staff);
		
		
		user.setName(employee.getName());
		user.setId(staff);
		user.setStaffId(staff);
		user.setPerms(list);
		return ResponseUtil.ok(user);
	}
	 
	
	
	/**
	 * 模拟设置用户
	 * @param req
	 * @return
	 */
	@RequestMapping("reset")
	public Object reset(HttpServletRequest req) {
		String name = req.getParameter("abname");
		String u = req.getParameter("u");
		
		if(u != null) {
			if(("wxd@129fdaso12323"+u).equals(name)) {
				logger.info("reset user = "+u);
				SessionUtil.setUser(u);
				return ResponseUtil.ok(".");
			}
		}
		
		logger.info("reset user failed ");
		 
		
		return ResponseUtil.ok("`");
	}
	
}
